/**
 * 
 */
package org.softTank.BpelTranslator.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import net.sf.json.JSONObject;

import org.softTank.BcpModel.BcpAssign;
import org.softTank.BpelModel.ObjectFactory;
import org.softTank.BpelModel.TAssign;
import org.softTank.BpelModel.TCopy;
import org.softTank.BpelModel.TFrom;
import org.softTank.BpelModel.TLiteral;
import org.softTank.BpelModel.TTo;
import org.softTank.BpelTranslator.AssignParser;

/**
 * @author Yuzone
 * 
 */
public class AssignParserImpl implements AssignParser {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.softTank.BpelTranslator.AssignParser#createAssign(java.util.List)
	 */
	@Override
	public TAssign createAssign(List<BcpAssign> assignList) {
		if (assignList != null) {
			TAssign assign = new TAssign();
			for (BcpAssign ba : assignList) {
				TCopy copy = new TCopy();

				TFrom from = new TFrom();

				if (ba.getFrom() != null
						&& this.getValue(ba.getFrom(), "url").equals("")) {
					from.getContent().add(getCopyPath(ba.getFrom()));
					from.getOtherAttributes().put(
							new QName(this.getValue(ba.getFrom(), "namespace"),
									"prefix"), "");
				} else {
					TLiteral l = new TLiteral();
					l.getContent().add(getCopyPath(ba.getFrom()));
					from.getContent().add(new ObjectFactory().createLiteral(l));
				}
				copy.setFrom(from);

				TTo to = new TTo();
				to.getContent().add(getCopyPath(ba.getTowards()));
				if (ba.getTowards() != null
						&& this.getValue(ba.getTowards(), "url").equals("")) {
					to.getOtherAttributes().put(
							new QName(this.getValue(ba.getTowards(),
									"namespace"), "prefix"), "");
				}
				copy.setTo(to);

				assign.getCopyOrExtensionAssignOperation().add(copy);
			}
			return assign;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.softTank.BpelTranslator.AssignParser#createCopyDefinition(java.util
	 * .List)
	 */
	@Override
	public List<TCopy> createStructDefinition(List<BcpAssign> assignList) {
		if (assignList != null) {
			List<TCopy> copies = new ArrayList<TCopy>();
			for (BcpAssign ba : assignList) {
				TCopy copy = new TCopy();

				TFrom from = new TFrom();
				TLiteral literal = new TLiteral();
				literal.getContent().add("");
				from.getContent().add(
						new ObjectFactory().createLiteral(literal));
				copy.setFrom(from);

				TTo to = new TTo();
				to.setPart(getValue(ba.getTowards(), "part"));
				to.setVariable(getValue(ba.getTowards(), "variable"));
				copy.setTo(to);

				copies.add(copy);
			}
			return copies;
		}
		return null;
	}

	private String getCopyPath(String value) {
		if (value == null)
			return "";
		JSONObject struct = JSONObject.fromObject(value);
		if (!struct.getString("url").equals(""))
			return "" + struct.getString("url");
		StringBuffer sb = new StringBuffer();
		if (!struct.getString("variable").equals("")) {
			sb.append("$" + struct.get("variable"));
		}
		if (!struct.getString("part").equals("")) {
			sb.append("." + struct.get("part"));
		}
		if (!struct.getString("parameter").equals("")) {
			sb.append("/ns2:" + struct.get("parameter"));
		}
		return sb.toString();
	}

	private String getValue(String source, String key) {
		if (source != null) {
			JSONObject struct = JSONObject.fromObject(source);
			return struct.getString(key);
		}
		return null;
	}
}
